from typing import Annotated, Any

from aiogram import Bot
from aiogram.types import BufferedInputFile
from dishka import FromDishka
from dishka.integrations.arq import inject

from weed_shop.core.models.enums.loyverse_sync import SyncType
from weed_shop.infrastructure.loyverse.sync_manager.main import LoyverseSyncManager
from weed_shop.worker.exception_handler import exception_handler


@exception_handler
@inject
async def loyverse_sync(
    context: dict[Any, Any],
    user_tg_id: int,
    bot: Annotated[Bot, FromDishka()],
    sync_manager: Annotated[LoyverseSyncManager, FromDishka()],
    sync_type: SyncType,
) -> None:
    if sync_type == SyncType.bot_to_loyverse:
        result = await sync_manager.sync_bot_to_loyverse()
    else:
        result = await sync_manager.sync_loyverse_to_bot()

    await bot.send_document(user_tg_id, BufferedInputFile(result.encode("utf8"), filename="Sync results.txt"))
